package com.penghaonan.appframework.base

import androidx.appcompat.app.AppCompatActivity
import com.penghaonan.appframework.reporter.Reporter
import com.penghaonan.appframework.utils.threadpool.AThreadPool
import com.penghaonan.appframework.utils.threadpool.IOThreadPool
import com.penghaonan.appframework.utils.threadpool.IThreadPoolHolder

open class BaseFrameworkActivity : AppCompatActivity(), IThreadPoolHolder {
    private var ioThreadPool: IOThreadPool? = null
    var resultCallback: ((Any?) -> Unit)? = null

    override fun onResume() {
        super.onResume()
        Reporter.getInstance().onActivityResume(this)
    }

    override fun onPause() {
        super.onPause()
        Reporter.getInstance().onActivityPause(this)
    }

    override fun getIOThreadPool(): AThreadPool {
        if (ioThreadPool == null) {
            ioThreadPool = IOThreadPool()
        }
        return ioThreadPool!!
    }

    override fun onDestroy() {
        super.onDestroy()
        if (ioThreadPool != null) {
            ioThreadPool!!.stop(true)
        }
    }

    fun finishAndCallback(result: Any) {
        resultCallback?.invoke(result)
        resultCallback = null
        finish()
    }
}
